//
//  SetupTableViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/7/1.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class SetupTableViewController: UITableViewController, UIActionSheetDelegate {
    @IBOutlet weak var on_off: UISwitch!
    @IBOutlet weak var pushON_Off: UISwitch!
    @IBOutlet weak var fontSizeLbl: UILabel!
    var sizeStr: String!
    var actionSheet: UIActionSheet? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        
        if (!(NSUserDefaults.standardUserDefaults().boolForKey("video_of_on"))) {
            self.on_off.on = false
        }else{
            self.on_off.on = true
        }

        if UIApplication.sharedApplication().currentUserNotificationSettings()?.types == UIUserNotificationType.None{
            self.pushON_Off.on = false
        }else{
            self.pushON_Off.on = true
        }

        if (actionSheet == nil) {
            self.actionSheet = UIActionSheet(title: "选择字体大小",delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles:"特大", "大", "中","小")
        }
        switch ApplicationDelegate.fontSize() {
        case "20":
            self.fontSizeLbl.text = "特大"
        case "18":
            self.fontSizeLbl.text = "大"
        case "16":
            self.fontSizeLbl.text = "中"
        case "14":
            self.fontSizeLbl.text = "小"
        default:
            self.fontSizeLbl.text = "中"
        }
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = NAVIGATIONBAR_RED_COLOR
        if (!(NSUserDefaults.standardUserDefaults().boolForKey("video_of_on"))) {
            self.on_off.on = false
        }else{
            self.on_off.on = true
        }
        
        if UIApplication.sharedApplication().currentUserNotificationSettings()?.types == UIUserNotificationType.None{
            self.pushON_Off.on = false
        }else{
            self.pushON_Off.on = true
        }
        
        switch ApplicationDelegate.fontSize() {
        case "20":
            self.fontSizeLbl.text = "特大"
        case "18":
            self.fontSizeLbl.text = "大"
        case "16":
            self.fontSizeLbl.text = "中"
        case "14":
            self.fontSizeLbl.text = "小"
        default:
            self.fontSizeLbl.text = "中"
        }


        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK:actionSheetDelegate
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex == 1 {
            //大
            print("特大")
            self.fontSizeLbl.text = "特大"
            NSUserDefaults.standardUserDefaults().setObject("20" , forKey:"fontSizeDefaut")
            
        }else if buttonIndex == 2 {
            //大
            print("大")
             self.fontSizeLbl.text = "大"
       NSUserDefaults.standardUserDefaults().setObject("18" , forKey:"fontSizeDefaut")
            
        }else if buttonIndex == 3{
            //中
            print("中")
             self.fontSizeLbl.text = "中"
            NSUserDefaults.standardUserDefaults().setObject("16" , forKey:"fontSizeDefaut")
           
        }else{
            //小
            print("小")
             self.fontSizeLbl.text = "小"
           NSUserDefaults.standardUserDefaults().setObject("14" , forKey:"fontSizeDefaut")
        }
        
    }
   

    @IBAction func stateChanged(switchState: UISwitch) {
        if switchState.on {
            
            NSUserDefaults.standardUserDefaults().setBool(true, forKey:"video_of_on")
            NSUserDefaults.standardUserDefaults().synchronize()
        } else {
            
            NSUserDefaults.standardUserDefaults().setBool(false, forKey:"video_of_on")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
    }
    @IBAction func pushChanged(switchState: UISwitch){

        let alertController: UIAlertController = UIAlertController.init(title: "提示", message: "请到设置中进行修改", preferredStyle: UIAlertControllerStyle.Alert)
       
        alertController.addAction(UIAlertAction(title: "确定", style: .Cancel, handler: {(action: UIAlertAction) -> Void in
            print("点击确定")
        }))
        
        self.presentViewController(alertController, animated: true, completion: nil)
        
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 0{
            self.actionSheet!.showInView(self.view!)
        }
        if indexPath.row == 1{
            // 取出cache文件夹路径
            let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
            // 打印路径,需要测试的可以往这个路径下放东西
            print(cachePath)
            // 取出文件夹下所有文件数组
            let files = NSFileManager.defaultManager().subpathsAtPath(cachePath!)
            // 用于统计文件夹内所有文件大小
            var big = Int();
            
            
            // 快速枚举取出所有文件名
            for p in files!{
                // 把文件名拼接到路径中
                let path = cachePath!.stringByAppendingFormat("/\(p)")
                // 取出文件属性
                let floder = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
                // 用元组取出文件大小属性
                for (abc,bcd) in floder {
                    // 只去出文件大小进行拼接
                    if abc == NSFileSize{
                        big += bcd.integerValue
                    }
                }
            }
            
            // 提示框
            let message = "\(big/(1024*1024))M缓存"
            let alert = UIAlertController(title: "清除缓存", message: message, preferredStyle: UIAlertControllerStyle.Alert)
            
            let alertConfirm = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default) { (alertConfirm) -> Void in
                // 点击确定时开始删除
                for p in files!{
                    // 拼接路径
                    let path = cachePath!.stringByAppendingFormat("/\(p)")
                    // 判断是否可以删除
                    if(NSFileManager.defaultManager().fileExistsAtPath(path)){
                        // 删除
                        //try! NSFileManager.defaultManager().removeItemAtPath(path)
                        do {
                            try NSFileManager.defaultManager().removeItemAtPath(path)
                        } catch {
                            
                        }
                    }
                }
            }
            alert.addAction(alertConfirm)
            let cancle = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel) { (cancle) -> Void in
                
            }
            alert.addAction(cancle)
            // 提示框弹出
            presentViewController(alert, animated: true) { () -> Void in
                
            }
            //self.fileSizeOfCache()
            //self.clearCache()
        }
    }
    
    func clearCache() {
        
        // 取出cache文件夹目录 缓存文件都在这个目录下
        let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
        
        // 取出文件夹下所有文件数组
        let fileArr = NSFileManager.defaultManager().subpathsAtPath(cachePath!)
        
        // 遍历删除
        for file in fileArr! {
            
            let path = cachePath?.stringByAppendingString("/\(file)")
            if NSFileManager.defaultManager().fileExistsAtPath(path!) {
                
                do {
                    try NSFileManager.defaultManager().removeItemAtPath(path!)
                } catch {
                    
                }
            }
        }
    }
    func fileSizeOfCache()-> Int {
        
        // 取出cache文件夹目录 缓存文件都在这个目录下
        let cachePath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first
        //缓存目录路径
        print(cachePath)
        
        // 取出文件夹下所有文件数组
        let fileArr = NSFileManager.defaultManager().subpathsAtPath(cachePath!)
        
        //快速枚举出所有文件名 计算文件大小
        var size = 0
        for file in fileArr! {
            
            // 把文件名拼接到路径中
            let path = cachePath?.stringByAppendingString("/\(file)")
            // 取出文件属性
            let floder = try! NSFileManager.defaultManager().attributesOfItemAtPath(path!)
            // 用元组取出文件大小属性
            for (abc, bcd) in floder {
                // 累加文件大小
                if abc == NSFileSize {
                    size += bcd.integerValue
                }
            }
        }
        
        let mm = size / 1024 / 1024
        
        return mm
    }

    
}
